<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>管理定区/调度排班</title>
	<!-- 导入jquery核心类库 -->
	<script type="text/javascript" src="../../js/jquery-1.8.3.js"></script>
	<!-- 导入easyui类库 -->
	<link rel="stylesheet" type="text/css" href="../../js/easyui/themes/default/easyui.css">
	<link rel="stylesheet" type="text/css" href="../../js/easyui/themes/icon.css">
	<link rel="stylesheet" type="text/css" href="../../js/easyui/ext/portal.css">
	<link rel="stylesheet" type="text/css" href="../../css/default.css">
	<script type="text/javascript" src="../../js/easyui/jquery.easyui.min.js"></script>
	<script type="text/javascript" src="../../js/easyui/ext/jquery.portal.js"></script>
	<script type="text/javascript" src="../../js/easyui/ext/jquery.cookie.js"></script>
	<script src="../../js/easyui/locale/easyui-lang-zh_CN.js" type="text/javascript"></script>
	<script type="text/javascript">
        $(function(){
            // 先将body隐藏，再显示，不会出现页面刷新效果
            $("body").css({visibility:"visible"});

            // 定区数据表格   最外面的大表
            $('#grid').datagrid( {
                iconCls : 'icon-forward',
                fit : true,
                border : true,
                rownumbers : true,
                striped : true,
                pageList: [30,50,100],
                pagination : true,
                toolbar : toolbar,
                url : "../../fixedArea_findAll.action",
                idField : 'id',
                columns : columns,
                onDblClickRow : doDblClickRow,
				singleSelect:true
            });

            //定义底部关联分区数据表格
            $('#association_subarea').datagrid( {
                pageList: [30,50,100],
                pagination : true,
                fit : true,
                border : true,
                rownumbers : true,
                striped : true,
                url : "../../fixedArea_findHasAssociationFixedAreaSubArea.action",
                columns : [ [{
                    field : 'id',
                    title : '分区编号',
                    width : 120,
                    align : 'center'
                },{
                    field : 'province',
                    title : '省',
                    width : 120,
                    align : 'center',
                    formatter : function(data,row ,index){
                        if(row.area!=null){
                            return row.area.province;
                        }
                        return "";
                    }
                }, {
                    field : 'city',
                    title : '市',
                    width : 120,
                    align : 'center',
                    formatter : function(data,row ,index){
                        if(row.area!=null){
                            return row.area.city;
                        }
                        return "";
                    }
                }, {
                    field : 'district',
                    title : '区',
                    width : 120,
                    align : 'center',
                    formatter : function(data,row ,index){
                        if(row.area!=null){
                            return row.area.district;
                        }
                        return "";
                    }
                }, {
                    field : 'keyWords',
                    title : '关键字',
                    width : 120,
                    align : 'center'
                } , {
                    field : 'assistKeyWords',
                    title : '辅助关键字',
                    width : 200,
                    align : 'center'
                }] ]
            });

            //定义底部关联客户数据表格
            $('#association_customer').datagrid( {
                pageList: [30,50,100],
                pagination : true,
                fit : true,
                border : true,
                rownumbers : true,
                striped : true,
                url : "../../fixedArea_findHasAssociationFixedAreaCustomers.action",
                columns : [[{
                    field : 'id',
                    title : '客户编号',
                    width : 120,
                    align : 'center'
                },{
                    field : 'username',
                    title : '客户名称',
                    width : 120,
                    align : 'center'
                }, {
                    field : 'company',
                    title : '所属单位',
                    width : 120,
                    align : 'center'
                }]]
            });
			//关联底部关联快递员数据表格
            $('#association_courier').datagrid( {
                pageList: [30,50,100],
                pagination : true,
                fit : true,
                border : true,
                rownumbers : true,
                striped : true,
                url : "../../fixedArea_findHasAssociationFixedAreaCouriers.action",
                columns : [ [ {
                    field : 'courierNum',
                    title : '工号',
                    width : 80,
                    align : 'center'
                },{
                    field : 'name',
                    title : '姓名',
                    width : 80,
                    align : 'center'
                }, {
                    field : 'telephone',
                    title : '手机号',
                    width : 120,
                    align : 'center'
                },{
                    field : 'standard.name',
                    title : '取派标准',
                    width : 120,
                    align : 'center',
                    formatter : function(data,row, index){
                        if(row.standard != null){
                            return row.standard.name;
                        }
                        return "";
                    }
                }, {
                    field : 'type',
                    title : '取派员类型',
                    width : 120,
                    align : 'center'
                }, {
                    field : 'company',
                    title : '所属单位',
                    width : 200,
                    align : 'center'
                } ] ]
            });
            $("#fixedAreaNotCouriers").datagrid({
                    pageList: [30,50,100],
                    pagination : true,
                    fit : true,
                    border : true,
                    rownumbers : true,
                    striped : true,
                    url : "../../fixedArea_findNoAssociationCouriers.action",
                    columns:[[{
                        field: 'id',
                        title: '编号',
                        width: 80,
                        align: 'center',
                        checkbox: true
                    }, {
                        field: 'courierNum',
                        title: '工号',
                        width: 80,
                        align: 'center'
                    }, {
                        field: 'name',
                        title: '姓名',
                        width: 80,
                        align: 'center'
                    }, {
                        field: 'telephone',
                        title: '手机号',
                        width: 120,
                        align: 'center'
                    }, {
                        field: 'standard.name',
                        title: '取派标准',
                        width: 120,
                        align: 'center',
                        formatter: function (data, row, index) {
                            if (row.standard != null) {
                                return row.standard.name;
                            }
                            return "";
                        }
                    }, {
                        field: 'type',
                        title: '取派员类型',
                        width: 120,
                        align: 'center'
                    }, {
                        field: 'company',
                        title: '所属单位',
                        width: 200,
                        align: 'center'

                    }]],
                    toolbar:[{
                        id: 'button-gl',
                        text: '关联',
                        iconCls: 'icon-add',
                        handler: doAss
                    }]
                }
            );
            $("#fixedAreaCouriers").datagrid({
                    pageList: [30,50,100],
                    pagination : true,
                    fit : true,
                    border : true,
                    rownumbers : true,
                    striped : true,
                    url : "../../fixedArea_findHasAssociationFixedAreaCouriers.action",
                    columns: [[{
                        field: 'id',
                        title: '编号',
                        width: 80,
                        align: 'center',
                        checkbox: true
                    },
                        {
                            field: 'courierNum',
                            title: '工号',
                            width: 80,
                            align: 'center'
                        }, {
                            field: 'name',
                            title: '姓名',
                            width: 80,
                            align: 'center'
                        }, {
                            field: 'telephone',
                            title: '手机号',
                            width: 120,
                            align: 'center'
                        }, {
                            field: 'standard.name',
                            title: '取派标准',
                            width: 120,
                            align: 'center',
                            formatter: function (data, row, index) {
                                if (row.standard != null) {
                                    return row.standard.name;
                                }
                                return "";
                            }
                        }, {
                            field: 'type',
                            title: '取派员类型',
                            width: 120,
                            align: 'center'
                        }, {
                            field: 'company',
                            title: '所属单位',
                            width: 200,
                            align: 'center'
                        }]],
                    toolbar:[{
                        id: 'button-qxgl',
                        text: '取消关联',
                        iconCls: 'icon-remove',
                        handler: cancelAss
                    }]

                }
            );
            // 添加、修改定区
            $('#addWindow').window({
                title: '添加修改定区',
                width: 600,
                modal: true,
                shadow: true,
                closed: true,
                height: 400,
                resizable:false
            });


            // 点击 添加定区的保存按钮，实现添加定区
            $("#save").click(function(){
                // 先校验表单
                if($("#fixedAreaForm").form('validate')) {

                    $("#fixedAreaForm").submit();
                }else {
                    // 提示用户
                    $.messager.alert("警告","表单存在非法数据项！","warning");
                }
            });


        });

        // 定义列
        var columns = [ [ {
            field : 'id',
            title : '编号',
            width : 80,
            align : 'center',
            checkbox:true
        },{
            field : 'fixedAreaNum',
            title : '定区编号',
            width : 120,
            align : 'center',
            formatter : function(value,row,index){
                return row.id;
            }
        },{
            field : 'fixedAreaName',
            title : '定区名称',
            width : 120,
            align : 'center'
        }, {
            field : 'fixedAreaLeader',
            title : '负责人',
            width : 120,
            align : 'center'
        }, {
            field : 'telephone',
            title : '联系电话',
            width : 120,
            align : 'center'
        }, {
            field : 'company',
            title : '所属公司',
            width : 120,
            align : 'center'
        } ] ];

        //工具栏
        var toolbar = [ {
            id : 'button-add',
            text : '增加',
            iconCls : 'icon-add',
            handler : doAdd
        }, {
            id : 'button-edit',
            text : '修改',
            iconCls : 'icon-edit',
            handler : doEdit
        },{
            id : 'button-association',
            text : '关联客户',
            iconCls : 'icon-sum',
            handler : doAssociationCustomers
        },{
            id : 'button-association-courier',
            text : '关联快递员',
            iconCls : 'icon-sum',
            handler : function(){
                var rows = $("#grid").datagrid('getSelections');
                selId=rows[0].id;
                // 只选择了一行
                if (rows.length == 1) {
                    $("#fixedAreaNotCouriers").datagrid("load", {'id': selId});
                    $("#fixedAreaCouriers").datagrid("load", {'id': selId});
                    // 弹出关联窗口
                    $('#courierWindow').window('open');
                } else {
                    // 没有选 或者 选了多个定区
                    $.messager.alert('警告', '必须选择一个定区', 'warning');
                }



            }
        },{
            id : 'button-association',
            text : '关联分区',
            iconCls : 'icon-sum',
            handler : function(){
               
            }
        }];

        //----点击工具栏中按钮要执行的方法----开始
        function doAdd(){
            $('#addWindow').window("open");
        }
        
        function doAss() {
            var rows = $("#fixedAreaNotCouriers").datagrid("getSelections");
            var array=new Array();
            for(var i=0;i<rows.length;i++){
                array.push(rows[i].id);
			}
            var courierIds = array.join(",");
			alert(courierIds);
			$.post("../../fixedArea_associationCourierToFixedArea.action",{"id":selId,"courierIds":courierIds},
				function (data) {
					if(data=="success"){
					    alert("添加成功");
					}else{
					    alert("添加失败");
					}
                },"json");
			$("#fixedAreaNotCouriers").datagrid("reload");
            $("#fixedAreaCouriers").datagrid("reload");

        }
        
        function cancelAss() {
            /*var rows = $("#fixedAreaCouriers").datagrid("getSelections");
            var array=new Array();
            for(var i=0;i<rows.length;i++){
                array.push(rows[i].id);
            }

            var courierIds = array.join(",");
            alert(courierIds);*/
            var rows = $("#fixedAreaCouriers").datagrid("getSelections");
            var array=new Array();
            for(var i=0;i<rows.length;i++){
                array.push(rows[i].id);
            }
            var courierIds = array.join(",");
            alert(courierIds);

            $.post("../../fixedArea_cancelAssociationCourierToFixedArea.action",{"id":selId,"courierIds":courierIds},
                function (data) {
                    if(data=="success"){
                        alert("添加成功");
                    }else{
                        alert("添加失败");
                    }
                },"json");
            $("#fixedAreaNotCouriers").datagrid("reload");

            $("#fixedAreaCouriers").datagrid("reload");


        }

        function doEdit(){
           var rows= $("#grid").datagrid("getSelections");

           if(rows.length!=1){
               $.messager.alert('警告','只能选择一行数据修改','warning');
		   }
		   var row = rows[0];
           $("#fixedAreaForm").form('load',row);

            $('#addWindow').window("open");

        }

        function doDblClickRow(){
            var rows = $("#grid").datagrid("getSelections");
            if(rows.length==1) {

                $("#customerFixedAreaId").val(rows[0].id);
            	$("#association_customer").datagrid({
            	    iconCls : 'icon-forward',
                    fit : true,
                    border : true,
                    rownumbers : true,
                    striped : true,
                    pageList: [30,50,100],
                    pagination : true,
					url:'../../fixedArea_findHasAssociationFixedAreaCustomers.action?id='+rows[0].id

            	});

//关联用户
                $("#association_customer").datagrid({
                    iconCls : 'icon-forward',
                    fit : true,
                    border : true,
                    rownumbers : true,
                    striped : true,
                    pageList: [30,50,100],
                    pagination : true,
                    url:'../../fixedArea_findHasAssociationFixedAreaCustomers.action?id='+rows[0].id
                });


               $("#association_customer").datagrid({
                    iconCls : 'icon-forward',
                    fit : true,
                    border : true,
                    rownumbers : true,
                    striped : true,
                    pageList: [30,50,100],
                    pagination : true,
                    url:'../../fixedArea_findHasAssociationFixedAreaCustomers.action?id='+rows[0].id
                });

            };

            }
            //打开关联客户窗口
        function doAssociationCustomers(){
            var rows = $("#grid").datagrid("getSelections");
            if(rows.length==1) {
                alert(rows[0].id);
                $("#customerFixedAreaId").val(rows[0].id);

                $.post('../../fixedArea_findNoAssociationCustomers.action', function (data) {
                    $("#noassociationSelect").empty();
                    $(data).each(function () {

                        var option = $("<option value ='" + this.id + "'>" + this.username + "</option>");

                        $("#noassociationSelect").append(option);
                    });

                    $("#customerWindow").window("open");
                }, 'json');
                $.post('../../fixedArea_findHasAssociationFixedAreaCustomers.action', {"id": rows[0].id}, function (data) {
                    $("#customer_associationSelect").empty();
                    $(data).each(function () {
                        var option = $("<option value ='" + this.id + "'>" + this.username + this.address + "</option>");
                        $("#associationSelect").append(option);
                    });

                }, "json");



                $("#toRight").click(function () {
                    $("#associationSelect").append($("#noassociationSelect option:selected"));
                });

                $("#toLeft").click(function () {
                    $("#noassociationSelect").append($("#associationSelect option:selected"));
                });
                //点击关联
			$("#associationBtn").click(
			    function () {
                    $("#associationSelect").attr('selected','selected');
                    $("#noassociationSelect").attr('selected','selected');
                    $("#customerForm").submit();
                    $("#customerWindow").window("close");
                }
			);

            }}

       
        //----点击工具栏中按钮要执行的方法----结束

        // 将form请求数据转换为json对象
        $.fn.serializeJson = function(){
            var serializeObj={};
            var array=this.serializeArray();
            var str=this.serialize();
            $(array).each(function(){
                if(serializeObj[this.name]){
                    if($.isArray(serializeObj[this.name])){
                        serializeObj[this.name].push(this.value);
                    }else{
                        serializeObj[this.name]=[serializeObj[this.name],this.value];
                    }
                }else{
                    serializeObj[this.name]=this.value;
                }
            });
            return serializeObj;
        }


	</script>
</head>

<body class="easyui-layout" style="visibility:hidden;">
<div region="center" border="false">
	<table id="grid"></table>
</div>
<div region="south" border="false" style="height:150px" split="true">
	<div id="tabs" fit="true" class="easyui-tabs">
		<div title="关联快递员" id="courier" style="width:100%;height:100%;overflow:hidden">
			<table id="association_courier"></table>
		</div>
		<div title="关联客户" id="customers" style="width:100%;height:100%;overflow:hidden">
			<table id="association_customer"></table>
		</div>
		<div title="关联分区" id="subareas" style="width:100%;height:100%;overflow:hidden">
			<table id="association_subarea"></table>
		</div>
	</div>
</div>

<!-- 添加 修改分区 -->
<div class="easyui-window" title="定区添加修改" id="addWindow" collapsible="false" minimizable="false" maximizable="false" style="top:20px;left:200px">
	<div style="height:31px;overflow:hidden;" split="false" border="false">
		<div class="datagrid-toolbar">
			<a id="save" icon="icon-save" href="#" class="easyui-linkbutton" plain="true">保存</a>
		</div>
	</div>

	<div style="overflow:auto;padding:5px;" border="false">
		<form id="fixedAreaForm" action="../../fixedArea_save.action" method="post">
			<table class="table-edit" width="80%" align="center">
				<tr class="title">

					<td colspan="2">定区信息</td>
				</tr>
				<tr>
					<td>定区编码</td>
					<td>
						<input type="text" name="id" class="easyui-validatebox" required="true" />
					</td>
				</tr>

				<tr>
					<td>定区名称</td>
					<td>
						<input type="text" name="fixedAreaName" class="easyui-validatebox" required="true" />
					</td>
				</tr>
				<tr>
					<td>负责人</td>
					<td>
						<input name="fixedAreaLeader" class="easyui-validatebox" required="true"/>
					</td>
				</tr>
				<tr>
					<td>联系电话</td>
					<td>
						<input name="telephone" class="easyui-validatebox" required="true"/>
					</td>
				</tr>
				<tr>
					<td>所属公司</td>
					<td>
						<input name="company" class="easyui-validatebox" required="true"/>
					</td>
				</tr>
			</table>
		</form>
	</div>
</div>

<!-- 关联客户窗口 -->
<div class="easyui-window" title="关联客户窗口" id="customerWindow" modal="true" collapsible="false" closed="true" minimizable="false" maximizable="false" style="top:20px;left:200px;width: 700px;height: 300px;">
	<div style="overflow:auto;padding:5px;" border="false">
		<form id="customerForm" action="../../fixedArea_associationCustomersToFixedArea.action" method="post">
			<table class="table-edit" width="80%" align="center">
				<tr class="title">
					<td colspan="3">关联客户</td>
				</tr>
				<tr>
					<td>
						<!-- 存放定区编号 -->
						<input type="hidden" name="id" id="customerFixedAreaId" />
						<select id="noassociationSelect" name="noCustomerIds" multiple="multiple" size="10" style="width: 300px;"></select>
					</td>
					<td>
						<input type="button" value="》》" id="toRight">
						<br/>
						<input type="button" value="《《" id="toLeft">
					</td>
					<td>
						<select id="associationSelect" name="customerIds" multiple="multiple" size="10" style="width: 300px;"></select>
					</td>
				</tr>
				<tr>
					<td colspan="3"><a id="associationBtn" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-save'">关联客户</a> </td>
				</tr>
			</table>
		</form>
	</div>
</div>

<!-- 关联快递员窗口 -->
<!--<div class="easyui-window" title="关联快递员窗口" id="courierWindow" modal="true"
	 collapsible="false" closed="true" minimizable="false" maximizable="false" style="top:20px;left:200px;width: 600px;height: 100px;">
	<div style="overflow:auto;padding:5px;" border="false" region="center">
		<form id="courierForm"
			  action="../../fixedArea_associationCourierToFixedArea.action" method="post">
			<table class="table-edit" width="80%" align="center">
				&lt;!&ndash;<tr>
					<td>选择快递员</td>
					<td>
						&lt;!&ndash; 存放定区编号 &ndash;&gt;
						<input type="hidden" name="id" id="courierFixedAreaId" />
						<input type="text" name="courierId" class="easyui-combobox" required="true" style="width: 180px"
							   data-options="url:'../../courier_findnoassociation.action',
									   				 valueField:'id', textField:'info'" />
					</td>

					<td colspan="3"><a id="associationCourierBtn" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-save'">关联快递员</a> </td>
				</tr>&ndash;&gt;
			</table>

		</form>
	</div>
	<div style="overflow:auto;padding:5px;" border="false" region="south">
		<form id="nocourierForm"
			  action="../../fixedArea_associationCourierToFixedArea.action" method="post">
			<table class="table-edit" width="80%" align="center">

			</table>

		</form>
	</div>
</div>-->
<div class="easyui-window" title="关联快递员窗口" id="courierWindow" modal="true" collapsible="false" closed="true"
	 minimizable="false" maximizable="false" style="top:10px;left:200px;width: 820px;height: 485px;">
	<div class="easyui-layout" style="width: 100%;height: 100%">
		<input type="hidden" name="id">
		<div region="center" border="false">
			<table id="fixedAreaNotCouriers">

			</table>
		</div>
		<div region="south" border="false" style="height: 150px;" split="true">
			<table id="fixedAreaCouriers">
			</table>
		</div>
	</div>
</div>


<!-- 关联分区窗口 -->
<div class="easyui-window" title="关联分区窗口" id="subAreaWindow" modal="true"
	 collapsible="false" closed="true" minimizable="false" maximizable="false" style="top:20px;left:200px;width: 600px;height: 100px;">
	<div style="overflow:auto;padding:5px;" border="false">
		<form id="subAreaForm"
			  action="../../fixedArea_associationSubAreaToFixedArea.action" method="post">
			<table class="table-edit" width="100%" align="center">
				<tr>
					<td>选择分区</td>
					<td>
						<!-- 存放定区编号 -->
						<input type="hidden" name="id" id="subAreaFixedAreaId" />
						<input type="text" name="subAreaId" class="easyui-combobox" required="true" style="width: 320px"
							   data-options="url:'../../subArea_findnoassociation.action',
									   				 valueField:'id', textField:'info'" />
					</td>

					<td colspan="3"><a id="associationSubAreaBtn" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-save'">关联分区</a> </td>
				</tr>
			</table>
		</form>
	</div>
</div>
</body>

</html>